package com.gugee.tiktok.web.controller.web;
import com.gugee.common.web.util.CheckMobile;
import com.gugee.common.web.util.FormatUtil;
import com.gugee.common.web.config.BaseController;
import com.gugee.common.web.system.user.dto.UserReturnDto;
import com.gugee.common.web.system.user.model.User;
import com.gugee.common.web.system.user.repository.UserRoleRepository;
import com.gugee.common.web.system.user.service.UserService;
import com.xiguaji.boot.toolkit.HttpUtil;
import com.xiguaji.boot.toolkit.JsonUtil;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

public class TikTokWebController extends BaseController {


    @Autowired
    UserRoleRepository userRoleRepository;
    @Autowired
    UserService userService;
    @Value("${server.domain}")
    String serverDomain;

    protected UserReturnDto getUser() {
        String token = HttpUtil.getCookie(authorized);

        if (StringUtils.isEmpty(token)) {
            return null;
        }
        User user = JsonUtil.jsonParse(User.class, JsonUtil.parseJson(jwtUtil.getJwtUser(token).getUser()));
        return userService.getUser(user.getId());
    }

    protected long getRoleId(UserReturnDto user){
        long roleId = 1;

        val userRoleOptional = userRoleRepository.findTop1ByUserIdAndExpireTimeGreaterThanEqualOrderByRoleIdDesc(user.getParentId() == 0 ?user.getId():user.getParentId(), new Date());

        if(userRoleOptional.isPresent()){
            roleId = userRoleOptional.get().getRoleId();
        }
        return roleId;
    }

    protected ModelAndView getModel(int tab){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("isMobile", CheckMobile.check());
        modelAndView.addObject("tab",tab);
        modelAndView.addObject("contextPath",serverDomain);
        UserReturnDto user = getUser();
        modelAndView.addObject("login", user != null);
        modelAndView.addObject("user", user);
        modelAndView.addObject("myController",this);
        modelAndView.addObject("roleId",user != null ? getRoleId(user): user);
        return modelAndView;
    }

    protected ModelAndView getModel(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("isMobile", CheckMobile.check());
        modelAndView.addObject("contextPath",serverDomain);
        UserReturnDto user = getUser();
        modelAndView.addObject("login", user != null);
        modelAndView.addObject("user", user);
        modelAndView.addObject("myController",this);
        modelAndView.addObject("roleId",user != null ? getRoleId(user): user);
        return modelAndView;
    }

    public String getNumber(long num) {
        return FormatUtil.getNumber(num);
    }

    public String getNumber(double num) {
        return FormatUtil.getNumber(num);
    }
}
